<?php

namespace App\Http\Middleware;

use Closure;
use Nette\Mail\Message;
use Nette\Mail\SendmailMailer;
use Nette\Mail\SmtpMailer;

class EmailMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $rs = $next($request);

        $mail = new Message;
        $mail->setFrom('John <bluebell520@126.com>')
            ->addTo($request->user()->email)
            ->setSubject('测试邮件中间件')
            ->setBody("Hello, Your order has been accepted.");

        $mailer = new SmtpMailer(array(
            'host' => 'smtp.126.com',
            'username' => 'bluebell520@126.com',
            'password' => 'admin1234',
            'secure' => 'ssl',
        ));
        $mailer->send($mail);

        return $rs;
    }
}
